<?php

//发送短信
use service\HttpService;

function sendSms($mobile, $content, $type = 0) {
    $product = array(
        0 => '676767', //优质验证码
        1 => '887362', //优质通知
        2 => '435227' //商超会员营销
    );

    $url = 'http://www.ztsms.cn/sendNSms.do';
    $tkey = date('YmdHis');
    $data = array(
        'username'  => sysconf('ztsms_username'),
        'tkey'      => $tkey,
        'password'  => md5(md5(sysconf('ztsms_password')) . $tkey),
        'mobile'    => $mobile,
        'content'   => $content . '【' . sysconf('ztsms_sign') . '】',
        'productid' => $product[$type]
    );

    $res = HttpService::getRequest($url, $data);
    $res = explode(",", $res);
    if ($res[0] == 1) {
        return true;
    } else {
        return false;
    }

}

function get_client_ip() {
    static $ip = NULL;
    if ($ip !== NULL)
        return $ip;
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos = array_search('unknown', $arr);
        if (false !== $pos)
            unset($arr[$pos]);
        $ip = trim($arr[0]);
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
    return $ip;
}

//生成单号
function createNO($table, $field, $prefix) {

    $billno = date('YmdHis') . random(6, true);
    while (1) {
        $count = \think\Db::name($table)->where([$field => $billno])->count();
        if ($count <= 0) {
            break;
        }
        $billno = date('YmdHis') . random(6, true);
    }
    return $prefix . $billno;
}

//随机数
function random($length, $numeric = FALSE) {
    $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
    $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
    if ($numeric) {
        $hash = '';
    } else {
        $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
        $length--;
    }
    $max = strlen($seed) - 1;
    for ($i = 0; $i < $length; $i++) {
        $hash .= $seed{mt_rand(0, $max)};
    }
    return $hash;
}

function media($path) {
    $preg = "/^http(s)?:\\/\\/.+/";
    if (preg_match($preg, $path)) {
        return $path;
    } else {
        return 'http://miyou.8396048.com' . $path;
    }
}

//获取当前域名
function domain()
{
    $request = \think\Request::instance();
    $domain=$request->domain();
    return $domain;
}



//创建二维码
function createQrcode($id)
{
    $path = ROOT_PATH . "public/qrcode";
    if (!is_dir($path)) {
        mkdir($path);
    }
    vendor("phpqrcode.phpqrcode");

    $data ='';
    $file = 'code' . $id . '.png';
    $qrcode_file = $path . '/' . $file;
    $level = 'L';
    $size =4;
    if (!is_file($qrcode_file)) {
        \service\QrcodeService::png($data,$qrcode_file,$level,$size,2);
    }
    return domain() . '/qrcode/' . $file;



}


